<template>
  <el-dialog :visible="showDialogAssign" title="权限设置" @close="hclose">
    <el-tree
      ref="tree"
      node-key="id"
      :data="dataList"
      show-checkbox
      default-expand-all
      check-strictly
      :props="{label: 'name'}"
    />
    <el-button size="mini" type="primary" @click="hSave">保存</el-button>
  </el-dialog>
</template>

<script>
import { getPermissionList, assignPrem } from '@/api/permission'
import { getPermissById } from '@/api/role'
import { arrToTree } from '@/utils/index'
export default {
  props: {
    showDialogAssign: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      dataList: []

    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    async  hSave() {
      const keys = this.$refs.tree.getCheckedKeys()

      const res = await assignPrem({ id: this.curId, permIds: keys })
      this.$message.success(res.message)
      this.hclose()
    },
    // 封装点击的员工权限数据
    async getPermiss() {
      const res = await getPermissById(this.curId)

      // 数据回填
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    hclose() {
      this.$emit('update:showDialogAssign', false)
    },
    async  loadPermission() {
      const res = await getPermissionList()
      this.dataList = arrToTree(res.data, 0)

      console.log(res)
    }

  }
}
</script>

<style>

</style>
